package com.luocj.muke.demo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextPaint;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luocj.muke.demo.R;
import com.luocj.muke.demo.activity.CountDownListActivity;
import com.luocj.muke.demo.activity.CustomIndicatorActivity;
import com.luocj.muke.demo.activity.LazyloadActivity;
import com.luocj.muke.demo.activity.LineChartActivity;
import com.luocj.muke.demo.activity.Main2Activity;
import com.luocj.muke.common.dialog.dialog.BaseDialog;
import com.luocj.muke.common.dialog.dialog.CustomDialog;
import com.luocj.muke.common.dialog.dialog.ViewConvertListener;
import com.luocj.muke.common.dialog.dialog.ViewHolder;
import com.luocj.muke.demo.activity.ShopcartActivity;
import com.luocj.muke.demo.activity.TouchEventActivity;
import com.luocj.muke.demo.utils.MyDialog;

import java.util.ArrayList;

public class HomeFragment extends BaseFragment implements View.OnClickListener {

    private static final String TAG = HomeFragment.class.getSimpleName();
    private View home;
    private boolean isEnd = false;
    private final ArrayList<Integer> widths = new ArrayList<>();
    private int edittextLength = 0;//输入商品名称长度

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        home = inflater.inflate(R.layout.fragment_home, container, false);
        initView(home);
        //
        //git dev1
        //git dev2 44444
        return home;
    }

    private void initView(View rootView) {
        rootView.findViewById(R.id.btn_lazyload).setOnClickListener(this);
        rootView.findViewById(R.id.btn_dialog).setOnClickListener(this);
        rootView.findViewById(R.id.btn_shop_cart).setOnClickListener(this);
        rootView.findViewById(R.id.btn_anim).setOnClickListener(this);
        rootView.findViewById(R.id.btn_okhttp).setOnClickListener(this);
        rootView.findViewById(R.id.btn_dialog_test).setOnClickListener(this);
        rootView.findViewById(R.id.btn_flow_layout).setOnClickListener(this);
        rootView.findViewById(R.id.btn_touch_event).setOnClickListener(this);
        rootView.findViewById(R.id.btn_count_down).setOnClickListener(this);
        rootView.findViewById(R.id.btn_line_chart).setOnClickListener(this);
        rootView.findViewById(R.id.btn_indicator).setOnClickListener(this);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    @Override
    protected void loadData() {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_anim:
                getActivity().startActivity(new Intent(getActivity(), Main2Activity.class));
                getActivity().overridePendingTransition(R.anim.rotate_in, 0);
                getActivity().finish();
                break;
            case R.id.btn_lazyload:
                getActivity().startActivity(new Intent(getActivity(), LazyloadActivity.class));
                break;
            case R.id.btn_dialog:
                CustomDialog
                        .init()
                        .setLayoutid(R.layout.dialog_progress)
                        .setConvertListener(new ViewConvertListener() {
                            @Override
                            protected void convertView(ViewHolder holder, BaseDialog dialog) {
                                TextView textview = holder.getView(R.id.tv_progress);
                                textview.setText("ewtwetwetwte");
                            }
                        })
                        .setWidth(200)
                        .setHeight(200)
                        .setDim(0.3f)
                        .setBottom(false)
                        .show(getFragmentManager());
                break;

            case R.id.btn_shop_cart:
                getActivity().startActivity(new Intent(getActivity(), ShopcartActivity.class));
                break;

            case R.id.btn_okhttp:
//                MyDialog myDialog = MyDialog.init(getActivity())
//                        .setLayoutId(R.layout.dialog_shop_cart);
//                myDialog.show();
//                final EditText edittext = myDialog.getInflate().findViewById(R.id.edit1);
//                RelativeLayout reEdittext = myDialog.findViewById(R.id.rl_re_edittext);
//                final TextView textviewDian = myDialog.getInflate().findViewById(R.id.text_dian);
//                ImageView imageview = myDialog.getInflate().findViewById(R.id.imageview);
//                textviewDian.setVisibility(View.INVISIBLE);
//                getEdittextWidth(edittext, reEdittext);
//
//                imageview.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        Log.i(TAG, "onClick: ");
//                        if (widths.get(0) < edittextLength) {
//                            textviewDian.setVisibility(View.VISIBLE);
//                            getFocouse(edittext);
//                        } else {
//                            textviewDian.setVisibility(View.INVISIBLE);
//                        }
//                    }
//                });
//                edittext.addTextChangedListener(new TextWatcher() {
//                    @Override
//                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//                    }
//
//                    @Override
//                    public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//                    }
//
//                    @Override
//                    public void afterTextChanged(Editable s) {
//                        edittextLength = (int) getTextLength(edittext);
//                        if (edittextLength - widths.get(0) < 0) {
//                            textviewDian.setVisibility(View.INVISIBLE);
//                        } else {
//                            textviewDian.setVisibility(View.VISIBLE);
//                        }
//                    }
//                });

                break;

            case R.id.btn_dialog_test:
                MyDialog dialog = MyDialog.init(getActivity())
                        .setLayoutId(R.layout.dialog_shop_cart);
                dialog.show();
                final EditText editextName = dialog.getInflate().findViewById(R.id.ed_good_name);//商品名称
                RelativeLayout relativeLayout = dialog.getInflate().findViewById(R.id.rl_reedit);//重新编辑
                final TextView textviewDian = dialog.getInflate().findViewById(R.id.tv_dian);//...
                ImageView imageview = dialog.getInflate().findViewById(R.id.imagview);
                textviewDian.setVisibility(View.GONE);
                getEdittextWidth(editextName, relativeLayout);
                imageview.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (widths.size() == 0) return;
                        if (widths.get(0) < edittextLength) {
                            textviewDian.setVisibility(View.VISIBLE);
                        } else {
                            textviewDian.setVisibility(View.GONE);
                        }
                        getFocouse(editextName);
                    }
                });
                editextName.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        edittextLength = (int) getTextLength(editextName);
                        if (widths.size() == 0) return;
                        if (widths.get(0) < edittextLength) {
                            textviewDian.setVisibility(View.VISIBLE);
                        } else {
                            textviewDian.setVisibility(View.GONE);
                        }
                    }
                });

                break;

            case R.id.btn_flow_layout:
                Log.i(TAG, "onClick: ");

                break;

            case R.id.btn_touch_event:
                getActivity().startActivity(new Intent(getActivity(),TouchEventActivity.class));

                break;
            case R.id.btn_count_down:
                getActivity().startActivity(new Intent(getActivity(),CountDownListActivity.class));
                break;
            case R.id.btn_line_chart:
                getActivity().startActivity(new Intent(getActivity(), LineChartActivity.class));
                break;
            case R.id.btn_indicator:
                getActivity().startActivity(new Intent(getActivity(),CustomIndicatorActivity.class));
                break;

        }
    }

    private void getEdittextWidth(final EditText edittext, final RelativeLayout reEdittext) {

        edittext.postDelayed(new Runnable() {
            @Override
            public void run() {
                int width = edittext.getWidth();
                int height = edittext.getHeight();

                int width1 = reEdittext.getWidth();
                int height1 = reEdittext.getHeight();
                widths.add(width);
                widths.add(width1);
                Log.i(TAG, "run: " + width + "width1:" + width1);

            }
        }, 500);
    }

    public interface onTextChangeListener {
        void OnTextChangeListener(String content);
    }

    private onTextChangeListener onTextChangeListener;

    public void setOnTextChangeListener(HomeFragment.onTextChangeListener onTextChangeListener) {
        this.onTextChangeListener = onTextChangeListener;
    }

    private float getTextLength(EditText editext) {
        TextPaint paint = editext.getPaint();
        return paint.measureText(editext.getText().toString());
    }

    private void getFocouse(EditText reEditext) {
        reEditext.setFocusable(true);
        reEditext.setFocusableInTouchMode(true);
        reEditext.requestFocus();
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        reEditext.setSelection(reEditext.getText().length());
    }


    @Override
    public void openCamera() {
        Log.i(TAG, "openCamera: ");
    }

    @Override
    public void openCameraFailed() {
        Log.i(TAG, "openCameraFailed: ");
    }

    @Override
    public void openCameraNeverAskAgain() {
        Log.i(TAG, "openCameraNeverAskAgain: ");
    }

    @Override
    public void sdcardPermission() {
        super.sdcardPermission();
        Log.i(TAG, "sdcardPermission: ");
    }

    @Override
    public void sdcardPermissionFailed() {
        super.sdcardPermissionFailed();
        Log.i(TAG, "sdcardPermissionFailed: ");
    }

    @Override
    public void sdcardPermissionNeverAskAgain() {
        Log.i(TAG, "sdcardPermissionNeverAskAgain: ");
    }
}
